package components
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.TextInput;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.Mysql;
	import tools.Tool;
	
	public class logIn extends Sprite{
		public var LOG_IN_STUDENT:String = "LOG_IN_STUDENT";
		public var LOG_IN_PREDAVAC:String = "LOG_IN_PREDAVAC";
		public var LOG_IN_ADMIN:String = "LOG_IN_ADMIN";
		private var panel:SuperPanel = new SuperPanel();
		private var usernameLabel:Label = new Label();
		private var passwordLabel:Label = new Label();
		private var registerLabel:Label = new Label();
		private var usernameTextInput:TextInput = new TextInput();
		private var passwordTextInput:TextInput = new TextInput();
		private var vb0:VBox = new VBox();
		private var hb0:HBox = new HBox();
		private var hb1:HBox = new HBox();
		private var submit:Button = new Button();
		private var _service:Mysql = new Mysql();
		private var _service1:Mysql = new Mysql();
		private var tool:Tool = new Tool();
		private var registerPanel:SuperPanel;
			
		public function logIn(p:SuperPanel){
			panel = p;
			usernameLabel.text = "Korisnicko ime:   "
			usernameLabel.x = 20;
			usernameLabel.y = 20;
			passwordLabel.text = "Lozinka:             "
			passwordTextInput.displayAsPassword = true;
			submit.label = "Prijavi se";
			registerLabel.htmlText = "<b> -> Registriraj se</b>";
			registerLabel.setStyle("color", "0xFF0000");
			registerLabel.useHandCursor = true;
			registerLabel.mouseChildren = false;
			registerLabel.buttonMode = true;
			registerLabel.addEventListener(MouseEvent.CLICK, onRegister);
			vb0.x = 10;
			vb0.y = 40;
			vb0.width = 240;
			vb0.height = 110;
			usernameTextInput.width =124;
			passwordTextInput.width = 122;
			
			// Ovo ce ici
			usernameTextInput.text = "";
			passwordTextInput.text = "";
			
			submit.width = vb0.width-5;
			hb0.width = vb0.width-5;
			hb0.height = 20;
			hb1.width = vb0.width-5;
			hb1.height = 20;
			show();
		}
		
		private function onRegister(e:MouseEvent):void{
			if(registerPanel==null){
				registerPanel = tool.createPanel("REGISTRACIJA", 0, 320, 195, 300, 150, true, false, "Registracija");
				panel.parentApplication.addChild(registerPanel);
				var dodaj:NewStudent = new NewStudent(registerPanel);
				registerPanel.canMaximize(false);
				registerPanel.canMinimize(false);
				registerPanel.canClose(true);
				registerPanel.addEventListener(SuperPanel.WINDOW_CLOSED, onCloseRegister);
			}
		}
		
		private function onCloseRegister(e:Event):void{
			registerPanel = null;
		}
			
		public function show():void{
			panel.addChild(vb0);
			vb0.addChild(hb0);
			vb0.addChild(hb1);
			vb0.addChild(submit);
			hb0.addChild(usernameLabel);
			hb0.addChild(usernameTextInput);
			hb1.addChild(passwordLabel);
			hb1.addChild(passwordTextInput);
			vb0.addChild(registerLabel);
			submit.addEventListener(MouseEvent.CLICK, onLogIn);
		}
		
		public function onLogIn(e:MouseEvent):void{
            _service.sql = {type:"SELECT", a1:"k.username", a2:"k.tip", a3:"k.id_username", a4:"k.avatar", a5:"last_login_time", a6:"last_login_date", from:"korisnik k", where:"username=@"+usernameTextInput.text+"@ AND password=@"+passwordTextInput.text+"@"};
			_service.addEventListener(_service.MYSQL_RESULT, onResult);
			_service.startService();
		} 
	 	
	 	public function onResult(e:Event):void{
	 		trace(_service.xml);
	 		var xmlList:XMLList = new XMLList();
			xmlList = _service.xml.children();
	 		if(xmlList.length()>0){
	 			if(xmlList[0].username==usernameTextInput.text){
	 				panel.parentApplication.username=xmlList[0].id_username;
	 				panel.parentApplication.user=xmlList[0].username;
	 				panel.parentApplication.avatar=xmlList[0].avatar;
	 				panel.parentApplication.tip=xmlList[0].tip;
	 				panel.parentApplication.last_login_time=xmlList[0].last_login_time;
	 				panel.parentApplication.last_login_date=xmlList[0].last_login_date;
	                _service1.sql = {type:"UPDATE", tables:"korisnik k", s1:"k.last_login_date=@"+tool.currentDate()+"@", s2:"k.last_login_time=@"+tool.currentTime()+"@", where:"k.username=@"+xmlList[0].username+"@" };
					_service1.addEventListener(_service.MYSQL_RESULT, onResult1);
					_service1.startService();                
	 			}
	 		} else {
	 			tool.createAlert("Netacni podaci", "Korisnicko ime sa unesenom lozinkom ne postoji u bazi!");
	 		}
	 	}
	 	
	 	public function onResult1(e:Event):void{
	 		try{
	 			panel.parentApplication.removeChild(registerPanel);
	 			registerPanel = null;
			} catch (e:Error){
					
			}
	 		if(panel.parentApplication.tip==0) dispatchEvent(new MouseEvent(LOG_IN_ADMIN));
	 		if(panel.parentApplication.tip==1) dispatchEvent(new MouseEvent(LOG_IN_STUDENT));
	 		if(panel.parentApplication.tip==2) dispatchEvent(new MouseEvent(LOG_IN_PREDAVAC));
	 		panel.parentApplication.isConnected();
	 	}
	 	
	 	public function get service():Mysql{
			return _service;	 		
	 	}
	}
}